 
  

 






<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>

<!-- Mirrored from www.javapractices.com/topic/TopicAction.do;jsessionid=4FCCB481C702D708A7360133D128E359?Id=39 by HTTrack Website Copier/3.x [XR&CO'2010], Sun, 12 Jun 2011 17:27:29 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=UTF-8"><!-- /Added by HTTrack -->
<head>
 <title>
  Java Practices -> Prefer Collections over older classes
 </title>
 <link rel="stylesheet" type="text/css" href="../stylesheet8.css" media="all">
 
 <link rel="shortcut icon" href='../images/favicon.ico' type="image/vnd.microsoft.icon">
 <meta name="description" content="Concise presentations of java programming practices, tasks, and conventions, amply illustrated with syntax highlighted code examples.">
 
 <meta name='keywords' content='ArrayList,HashMap,HashSet,Hashtable,LinkedList,Properties,sort,sorting,Stack,TreeMap,TreeSet,Vector,collection,java,java programming,java practices,java idiom,java style,java design patterns,java coding conventions,'>
 
 
</head>
 
<body>


<div class='menu-bar'>
 
  <a href='../home/HomeAction.html' title='Table of Contents'>Home</a> |
  <a href='../vote/VoteSummaryAction-2.html' title='View Poll Results'>Poll</a> |
   
  <A href='../feedback/FeedbackAction451f-2.html?Operation=Show' title='Send Your Feedback'>Wiki</a> |
  <b><a href='../source/SourceAction-2.html' title='Grab Source Code'>Source Code</a></b><IMG class='no-margin' SRC="../images/goldstar.gif" ALT=""> |

  <a href='http://www.web4j.com/Java_Web_Application_Framework_Overview.jsp?From=1' title='Free Download - Java Web Application Framework'><b>WEB4J</b></a> |
  
  <a href='http://www.date4j.net/' title='Replacement for java.util.Date'><b>DATE4J</b></a> |

   <a href='../references/ReferencesAction-2.html' title='References'>Links</a>
   
  <form action='http://www.javapractices.com/search/SearchAction.do' method='get' class='search-form'>
   <input type='text' name='SearchTerms' value="" size=12 maxlength=50 class='search'>
   <input type='submit' value="Search">
  </form>
 
</div>

<P>



  

 






<p class="display-messages">

 

 

</p>


<div class="main-layout">
 
   

 




<div class='page-title'>Prefer Collections over older classes</div>

<div class='main-body'>
 
<br>Many programmers prefer the Java 
<a href="http://java.sun.com/javase/6/docs/technotes/guides/collections/index.html">Collections Framework</a> 
to arrays, <tt><a href="http://java.sun.com/javase/6/docs/api/java/util/Vector.html">Vector</a></tt>,
and <tt><a href="http://java.sun.com/javase/6/docs/api/java/util/Hashtable.html">Hashtable</a></tt>, for the following reasons :
<ul>
<li>
the intent of the Collections Framework is to replace the older, less comprehensive
tools</li>

<li>
synchronization can be easily controlled (<tt>Hashtable</tt> and <tt>Vector</tt>
are always synchronized)</li>

<li>
immutability can be easily controlled</li>

<li>
search and sort algorithms are ready made</li>

<li>
the type of a reference to a <tt><a href="http://java.sun.com/javase/6/docs/api/java/util/Collection.html">Collection</a></tt>
can be generic, while underlying implementation can be easily changed</li>

<li>
arrays are of fixed size, and are more error prone - off-by-one errors
with array indexes have always been a fruitful source of error</li>

<li>
<tt><a href="http://java.sun.com/javase/6/docs/api/java/util/Arrays.html#asList(T...)">Arrays.asList</a></tt>
may be used to pass an array to a method expecting a <tt>Collection</tt>
or a <tt>List</tt></li>

<li>
<tt><a href="http://java.sun.com/javase/6/docs/api/java/util/Collection.html">Collection</a>.toArray</tt>
may be used to convert a <tt>Collection</tt> to an <tt>Object[]</tt> or
a <tt>T[]</tt></li>

<li><tt><a href='http://java.sun.com/javase/6/docs/api/java/util/Collections.html#enumeration(java.util.Collection)'>Collections.enumeration</a></tt> and 
<tt><a href='http://java.sun.com/javase/6/docs/api/java/util/Collections.html#list(java.util.Enumeration)'>Collections.list</a></tt> may be used to convert between enumerations and collections

</ul>
In JDK 1.5, a strong case can be made that Collections should almost always
be preferred over arrays.
<p>Prior to JDK 1.5, the main disadvantage of using collections is that
they always hold an
<tt>Object</tt>, and retrieval of their contents requires
casting. Casting cannot be checked at compile-time. Arrays, on the other
hand, have the advantage of always being "parameterized" to hold objects
of a specific type (in all versions of the JDK), and this is verified by
the compiler. Thus, in older versions of the JDK, changing from a collection to an array forces one particular
kind of error to appear at compile-time instead of run-time.

<P>One minor advantage of arrays is that they are slightly easier to initialize. However, using
<tt><a href="http://java.sun.com/javase/6/docs/api/java/util/Arrays.html#asList(T...)">Arrays.asList</a></tt>
makes the difference trivial :
<br>
<PRE>

<span class='keyword'>import</span> java.util.*;

<span class='keyword'>public</span> <span class='keyword'>final</span> <span class='keyword'>class</span> ArrayInit {

  <span class='comment'>/** Initialize arrays and Lists.  */</span>
  <span class='keyword'>public</span> <span class='keyword'>static</span> <span class='keyword'>void</span> main(String... args){
    <span class='comment'>//Array initializers are compact
</span>    String[] paintings = {<span class='literal'>"oil"</span>, <span class='literal'>"watercolour"</span>};

    <span class='comment'>//Build a List using Arrays.asList(T...)
</span>    <span class='comment'>//This works for any type, not just for String
</span>    List&lt;String&gt; languages = Arrays.asList(
      <span class='literal'>"urdu"</span>, <span class='literal'>"hindi"</span>, <span class='literal'>"pali"</span>, <span class='literal'>"sanskrit"</span>
    );
    
    <span class='comment'>//Build a List in a more verbose way 
</span>    List&lt;String&gt; nicePlaces = <span class='keyword'>new</span> ArrayList&lt;String&gt;();
    nicePlaces.add(<span class='literal'>"luxembourg gardens"</span>);
    nicePlaces.add(<span class='literal'>"sea side"</span>);
    nicePlaces.add(<span class='literal'>"large magellanic cloud"</span>);
  }
} 
</PRE>
<br>
<br>
<br>

</div>




<div class='topic-section'>See Also :</div>
<div class='main-body'>
 
  
  <a href='TopicActionc6f5-2.html?Id=65'>Choosing the right Collection</a> <br>
 
  
  <a href='TopicAction56bf-2.html?Id=227'>Avoid basic style errors</a> <br>
 
</div>


<div class='topic-section'>Would you use this technique?</div>
<div class='main-body'>
  
  <form action="http://www.javapractices.com/vote/AddVoteAction.do" method='post'>
    Yes<input type='radio' name='Choice' value='Y' >
    &nbsp;&nbsp;No<input type='radio' name='Choice' value='N'>
    &nbsp;&nbsp;Undecided<input type='radio' name='Choice' value="?" >
    &nbsp;&nbsp;<input type=submit value="Vote" >
    <input type='hidden' name='Operation' value='Apply'>
    <input type='hidden' name='TopicId' value='39'>
  </form>
</div>

<div style='height:10.0em;'></div>

 
 
</div>

  

 





<div align='center' class='legalese'>  
&copy; 2011 Hirondelle Systems |
<a href='../source/SourceAction-2.html'><b>Source Code</b></a><IMG class='no-margin' SRC="../images/goldstar.gif" ALT=""> |
<a href="mailto:webmaster@javapractices.com">Contact</a> |
<a href="http://creativecommons.org/licenses/by-nc-sa/1.0/">License</a> |
<a href='../apps/cjp.rss'>RSS</a>
<!-- ukey="2AC36CD2" -->
<!-- ckey="16DF3D87" -->
<br>

 Individual code snippets can be used under this <a href='../LICENSE.txt'>BSD license</a> - Last updated on June 6, 2010.<br>
 Over 150,000 unique IPs last month - <span title='Java Practices 2.6.5, Mon May 16 00:00:00 EDT 2011'>Built with</span> <a href='http://www.web4j.com/'>WEB4J</a>.<br>
 - In Memoriam : Bill Dirani -
</div>

<script src="../../www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2633428-1";
urchinTracker();
</script>



</body>

<!-- Mirrored from www.javapractices.com/topic/TopicAction.do;jsessionid=4FCCB481C702D708A7360133D128E359?Id=39 by HTTrack Website Copier/3.x [XR&CO'2010], Sun, 12 Jun 2011 17:27:29 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=UTF-8"><!-- /Added by HTTrack -->
</html>
